package com.zx._06_JVM.chapter_03_jmm;

import com.zx.common.entity.User2;
import java.util.Date;
import org.openjdk.jol.info.ClassLayout;

public class JavaObjectLayout {

    public static void main(final String[] args) {
        System.out.println("############ 1. object 对象内存分布 ############");
        final Object obj = new Object();
        String printable = ClassLayout.parseInstance(obj).toPrintable();
        System.out.println(printable);

        System.out.println("############ 2. User2 对象内存分布 ############");
        final User2 user = new User2(1, "tom", new Date());
        printable = ClassLayout.parseInstance(user).toPrintable();
        System.out.println(printable);

        System.out.println("############ 3. 常量 int 内存分布 ############");
        System.out.println(ClassLayout.parseInstance(1).toPrintable());

        System.out.println("############ 4. 数组 内存分布 ############");
        int[] ints = new int[10];
        System.out.println(ClassLayout.parseInstance(ints).toPrintable());

        System.out.println("############ 5. 对象的数组（null） 内存分布 ############");
        Object[] objs = new Object[1000];
        System.out.println(ClassLayout.parseInstance(objs).toPrintable());

        System.out.println("############ 6. 对象的数组（not null） 内存分布 ############");
        for (int i = 0; i < objs.length; i++) {
            objs[i] = new Object();
        }
        System.out.println(ClassLayout.parseInstance(objs).toPrintable());

        System.out.println("############ 7. 线程对象 内存分布 ############");
        Thread thread = new Thread(() -> {
            System.out.println(1111);
        });
        System.out.println(ClassLayout.parseInstance(thread).toPrintable());
    }
}
/*
############ 1. object 对象内存分布 ############
# WARNING: Unable to get Instrumentation. Dynamic Attach failed. You may add this JAR as -javaagent manually, or supply -Djdk.attach.allowAttachSelf
# WARNING: Unable to attach Serviceability Agent. sun.jvm.hotspot.memory.Universe.getNarrowOopBase()
java.lang.Object object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           05 00 00 00 (00000101 00000000 00000000 00000000) (5)
      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4        (object header)                           94 20 00 00 (10010100 00100000 00000000 00000000) (8340)
     12     4        (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

############ 2. User2 对象内存分布 ############
com.zx.common.entity.User2 object internals:
 OFFSET  SIZE                TYPE DESCRIPTION                               VALUE
      0     4                     (object header)                           05 00 00 00 (00000101 00000000 00000000 00000000) (5)
      4     4                     (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4                     (object header)                           88 8d 17 00 (10001000 10001101 00010111 00000000) (1543560)
     12     4                 int User2.age                                 10
     16     4   java.lang.Integer User2.id                                  1
     20     4    java.lang.String User2.name                                (object)
     24     4      java.util.Date User2.birtday                             (object)
     28     4                     (loss due to the next object alignment)
Instance size: 32 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

############ 3. 常量 int 内存分布 ############
java.lang.Integer object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           01 00 00 00 (00000001 00000000 00000000 00000000) (1)
      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4        (object header)                           c5 ed 05 00 (11000101 11101101 00000101 00000000) (388549)
     12     4    int Integer.value                             1
Instance size: 16 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total

############ 4. 数组 内存分布 ############
[I object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           01 00 00 00 (00000001 00000000 00000000 00000000) (1)
      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4        (object header)                           3b 56 07 00 (00111011 01010110 00000111 00000000) (480827)
     12     4        (object header)                           0a 00 00 00 (00001010 00000000 00000000 00000000) (10)
     16    40    int [I.<elements>                             N/A
Instance size: 56 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total

############ 5. 对象的数组（null） 内存分布 ############
[Ljava.lang.Object; object internals:
 OFFSET  SIZE               TYPE DESCRIPTION                               VALUE
      0     4                    (object header)                           01 00 00 00 (00000001 00000000 00000000 00000000) (1)
      4     4                    (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4                    (object header)                           12 21 00 00 (00010010 00100001 00000000 00000000) (8466)
     12     4                    (object header)                           e8 03 00 00 (11101000 00000011 00000000 00000000) (1000)
     16  4000   java.lang.Object Object;.<elements>                        N/A
Instance size: 4016 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total

############ 6. 对象的数组（not null） 内存分布 ############
[Ljava.lang.Object; object internals:
 OFFSET  SIZE               TYPE DESCRIPTION                               VALUE
      0     4                    (object header)                           01 00 00 00 (00000001 00000000 00000000 00000000) (1)
      4     4                    (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4                    (object header)                           12 21 00 00 (00010010 00100001 00000000 00000000) (8466)
     12     4                    (object header)                           e8 03 00 00 (11101000 00000011 00000000 00000000) (1000)
     16  4000   java.lang.Object Object;.<elements>                        N/A
Instance size: 4016 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total



############ 7. 线程对象 内存分布 ############
java.lang.Thread object internals:
 OFFSET  SIZE                                        TYPE DESCRIPTION                               VALUE
      0     4                                             (object header)                           05 00 00 00 (00000101 00000000 00000000 00000000) (5)
      4     4                                             (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4                                             (object header)                           7d 34 02 00 (01111101 00110100 00000010 00000000) (144509)
     12     4                                         int Thread.priority                           5
     16     8                                        long Thread.eetop                              0
     24     8                                        long Thread.stackSize                          0
     32     8                                        long Thread.tid                                15
     40     4                                         int Thread.threadStatus                       0
     44     1                                     boolean Thread.daemon                             false
     45     1                                     boolean Thread.stillborn                          false
     46     2                                             (alignment/padding gap)
     48     4                            java.lang.String Thread.name                               (object)
     52     4                          java.lang.Runnable Thread.target                             (object)
     56     4                       java.lang.ThreadGroup Thread.group                              (object)
     60     4                       java.lang.ClassLoader Thread.contextClassLoader                 (object)
     64     4          java.security.AccessControlContext Thread.inheritedAccessControlContext      (object)
     68     4        java.lang.ThreadLocal.ThreadLocalMap Thread.threadLocals                       null
     72     4        java.lang.ThreadLocal.ThreadLocalMap Thread.inheritableThreadLocals            null
     76     4                            java.lang.Object Thread.parkBlocker                        null
     80     4                    sun.nio.ch.Interruptible Thread.blocker                            null
     84     4                            java.lang.Object Thread.blockerLock                        (object)
     88     4   java.lang.Thread.UncaughtExceptionHandler Thread.uncaughtExceptionHandler           null
     92   132                                             (alignment/padding gap)
    224     8                                        long Thread.threadLocalRandomSeed              0
    232     4                                         int Thread.threadLocalRandomProbe             0
    236     4                                         int Thread.threadLocalRandomSecondarySeed     0
Instance size: 240 bytes
Space losses: 134 bytes internal + 0 bytes external = 134 bytes total
*/
